<template>
  <div class="login-color">
    <div class="login_box">
      <div class="avatar-box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form
        label-width="0px"
        id="login-form"
        :model="form"
        :rules="rules"
        ref="resetRef"
      >
        <el-form-item class="user" prop="username">
          <el-input
            v-model="form.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item class="paw" prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>
        <el-row class="btn">
          <el-button type="primary" @click="login">确定</el-button>

          <el-button type="info" @click="setRse">重置</el-button>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入名称", trigger: "blur" },
          { min: 3, max: 10, message: "名称3-10", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 16, message: "密码6-16", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    setRse() {
      // console.log(this);
      this.$refs.resetRef.resetFields();
    },
    login() {
      this.$refs.resetRef.validate(async (valid) => {
        // console.log(valid);
        if (!valid) return;
        const { data: res } = await this.$http.post("login", this.form);
        // console.log(res);
        if (res.meta.status !== 200) {
        //  return console.log('错误');
          this.$message.error("登录错误");
        }
        this.$message.success("登录成功");
        // 登录成功后把token保存在seccion中h
        window.sessionStorage.setItem("token",res.data.token)
        // 登陆成功后直接跳转到后台首页
        this.$router.push('/home')
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-color {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  position: absolute;
  width: 450px;
  height: 300px;
  background-color: #fff;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar-box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 5px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  #login-form {
    position: absolute;
    bottom: 0;
    padding: 20px;
    width: 100%;
    box-sizing: border-box;
    .user,
    .pwd {
      width: 100%;
    }
    .btn {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>